import Vue from 'vue'
import store from './store'
import Router from 'vue-router'
Vue.use(Router)

import home from './components/navmenu/home.vue'
// import table from './components/navmenu/table.vue'
// import form from './components/sun/form.vue'
// import login from './components/navmenu/login'
// import help from './components/navmenu/help'
// import about from './components/navmenu/about.vue'
// import noticelist from './components/navmenu/noticelist'
// import noticeinfo from './components/sun/noticeinfo.vue'
// import listinfo from './components/sun/listinfo.vue'
// import service from './components/navmenu/service.vue'
// import manage from './components/management/manage.vue'
// import managehome from './components/managesun/mangehome.vue'
// import managelist1 from './components/managesun/managelist1.vue'
// import managelist2 from './components/managesun/managelist2.vue'
// import managelist3 from './components/managesun/managelist3.vue'
// import revisdata from './components/managesun/revisdata.vue'
// import per1 from './components/managesun/per1.vue'
// import per2 from './components/managesun/per2.vue'
// import article from './components/managesun/article.vue'
// import text from './components/managesun/articleSun/text.vue'
// import category from './components/managesun/articleSun/category.vue'
// import edit from './components/managesun/articleSun/edit.vue'



var router = new Router({
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/home', component: home, meta: { keepalive: true } },
    { path: '/table', component: resolve => require(['./components/navmenu/table.vue'], resolve)  },
    { path: '/home/form', component: resolve => require(['./components/sun/form.vue'], resolve) },
    { path: '/login', component: resolve => require(['./components/navmenu/login'], resolve) },
    { path: '/help', component: resolve => require(['./components/navmenu/help'], resolve), meta: { keepalive: true }, meta: { keepalive: true } },
    { path: '/about', component: resolve => require(['./components/navmenu/about.vue'], resolve), meta: { keepalive: true } },
    { path: '/noticelist', component: resolve => require(['./components/navmenu/noticelist'], resolve) },
    { path: '/service', component: resolve => require(['./components/navmenu/service.vue'], resolve) },
    { path: '/noticelist/noticeinfo/:id', component: resolve => require(['./components/sun/noticeinfo.vue'], resolve) },
    { path: '/table/listinfo/:id', component: resolve => require(['./components/sun/listinfo.vue'], resolve) },
    { path: '/manage', redirect: '/manage/managehome' },
    {
      path: '/manage', component: resolve => require(['./components/management/manage.vue'], resolve), meta: { requiresAuth: true },
      children: [
        { path: 'managehome', component: resolve => require(['./components/managesun/mangehome.vue'], resolve), meta: { requiresAuth: true } },
        { path: 'list/1', component: resolve => require(['./components/managesun/managelist1.vue'], resolve), meta: { requiresAuth: true } },
        { path: 'list/2', component: resolve => require(['./components/managesun/managelist2.vue'], resolve), meta: { requiresAuth: true } },
        { path: 'list/3', component: resolve => require(['./components/managesun/managelist3.vue'], resolve), meta: { requiresAuth: true } },
        { path: 'revisdata', component: resolve => require(['./components/managesun/revisdata.vue'], resolve), meta: { requiresAuth: true } },
        { path: 'per/1', component: resolve => require(['./components/managesun/per1.vue'], resolve), meta: { superAdmin: true } },
        { path: 'per/2', component: resolve => require(['./components/managesun/per2.vue'], resolve), meta: { superAdmin: true } },
        { path: 'article', redirect: 'article/text', meta: { textAuth:true} },
        {
          path: 'article', component: resolve => require(['./components/managesun/article.vue'], resolve), children: [
            { path: 'text', component: resolve => require(['./components/managesun/articleSun/text.vue'], resolve), meta: { textAuth:true} },
            { path: 'category', component: resolve => require(['./components/managesun/articleSun/category.vue'], resolve), meta: { textAuth:true} },
            { path: 'edit', component: resolve => require(['./components/managesun/articleSun/edit.vue'], resolve), name: "edit", meta: { textAuth:true} },
          ]
        }
      ]
    },

    // {
    //   // path: '',
    //   // name: '',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   //component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    // }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {  // 判断该路由是否需要登录权限
    if (store.state.token && store.state.past === false) {  // 通过vuex state获取当前的token是否存在
      next();
    }
    else {
      next({
        path: '/login'
      })
    }
  } else if (to.meta.superAdmin) {   //超级管理员权限
    if (store.state.token && store.state.past === false && store.state.power > 2) {
      next();
    }
    else if (store.state.token && store.state.past === false) {
      next({
        path: '/manage'
      })
    }
    else {
      next({
        path: '/login'
      })
    }
  }else if (to.meta.textAuth) {   //文章管理权限
    if (store.state.token && store.state.past === false && store.state.power >= 2) {
      next();
    }
    else if (store.state.token && store.state.past === false) {
      next({
        path: '/manage'
      })
    }
    else {
      next({
        path: '/login'
      })
    }
  }
  else {
    next();
  }
})
export default router